import 'package:flutter/material.dart';
import 'package:test1/pages/utils/Toast.dart';
class Widget_WillPopScope_Page extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return Widget_WillPopScope_State();
  }
}

class Widget_WillPopScope_State extends State<Widget_WillPopScope_Page> {

  @override
  Widget build(BuildContext context) {
    DateTime lastTime;

    return WillPopScope(
      onWillPop: () async {
        if (lastTime == null || DateTime.now().difference(lastTime) > Duration(seconds: 1)) {
          lastTime = DateTime.now();
          Toast.toast(context, "双击退出");
          return false;
        }
        return true;
      },
      child: MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text("双击退出"),
          ),
        ),
      ),
    );
  }

}

/**
 *  导航返回拦截，避免用户误触返回按钮而导致APP退出
 * const WillPopScope({
    Key key,
    @required this.child,
    @required this.onWillPop,//当用户点击返回按钮时调用（包括导航返回按钮及Android物理返回按钮）,返回 Future.value(false); 表示不退出;返回 Future.value(true); 表示退出.
    })
 */
